package com.example.dump.service;

import cn.hutool.extra.spring.SpringUtil;
import com.example.dump.config.MysqlDataBaseInfoConfig;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Slf4j
@Service
@RequiredArgsConstructor
public class ScheduleManage {

    private final  MysqlDataBaseInfoConfig mysqlDataBaseInfoConfig;

    @Value("${dump.mysql.flag:false}")
    private boolean dumpMysqlFlag;


    @Scheduled(cron = "${dump.mysql.cron}")
    public void dumpMysql() {

        if (!dumpMysqlFlag){
            log.info("dump.mysql.flag is {}", false);
            return;
        }
        DumpDataBaseService dumpDataBaseService = SpringUtil.getBean("mysqlDumpDataBaseServiceImpl", DumpDataBaseService.class);
        try {
            dumpDataBaseService.dump(mysqlDataBaseInfoConfig,null);
        } catch (IOException | InterruptedException e) {
            throw new RuntimeException(e);
        }
        log.info("dumpMysql finish ....");
    }
}
